package com.joysuch.wwyt.edu.schedule;

import cn.hutool.core.collection.CollectionUtil;
import com.joysuch.wwyt.edu.entity.EduTrainningRecord;
import com.joysuch.wwyt.edu.entity.EduTrainningRecordUser;
import com.joysuch.wwyt.edu.enums.TrainingQualifiedStandard;
import com.joysuch.wwyt.edu.enums.TrainingUserSignStatus;
import com.joysuch.wwyt.edu.mapper.EduTrainningRecordMapper;
import com.joysuch.wwyt.edu.mapper.EduTrainningRecordUserMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;

@Slf4j
@Component
public class EduTrainingResultSchedule {

    @Autowired
    private EduTrainningRecordMapper eduTrainningRecordMapper;
    @Autowired
    private EduTrainningRecordUserMapper eduTrainningRecordUserMapper;


    @Scheduled(cron = "0 0/30 * * * ?")
//    @Scheduled(cron = "0/10 * * * * ?")
    public void run() {
        List<EduTrainningRecord> eduTrainningRecords = eduTrainningRecordMapper.selectEndTraining();
        if (CollectionUtil.isEmpty(eduTrainningRecords)){
            return;
        }
        eduTrainningRecords.forEach(eduTrainningRecord -> {
            //培训活动已结束 培训结果为空的用户
            List<EduTrainningRecordUser> users = eduTrainningRecordUserMapper.selectEndUser(eduTrainningRecord.getId());
            if (eduTrainningRecord.getTrainingQualifiedStandard() == TrainingQualifiedStandard.FINISH_SIGN_IN.getId()) {
                finishSign(users);
            } else if (eduTrainningRecord.getTrainingQualifiedStandard() == TrainingQualifiedStandard.PASS_EXAM.getId()) {
                passExam(users, eduTrainningRecord.getExamId());
            } else if (eduTrainningRecord.getTrainingQualifiedStandard() == TrainingQualifiedStandard.FINISH_COURSE.getId()) {
                finishCourse(users);
            } else if (eduTrainningRecord.getTrainingQualifiedStandard() == TrainingQualifiedStandard.FINISH_COURSE_EXAM.getId()) {
                finishCourseAndPassExam(users, eduTrainningRecord.getExamId());
            } else if (eduTrainningRecord.getTrainingQualifiedStandard() == TrainingQualifiedStandard.FINISH_SIGN_IN_EXAM.getId()) {
                finishSignAndPassExam(users,eduTrainningRecord.getExamId());
            }
        });

    }
    /**
     * 培训合格标准为完成签到+考试通过
     *
     * @param users
     */
    private void finishSignAndPassExam(List<EduTrainningRecordUser> users, Long examId) {
        users.forEach(user -> {
            List<Long> ids = new ArrayList<>(1);
            ids.add(user.getUserId());
            //完成签到+考试通过
            if (TrainingUserSignStatus.OFFLINE.getId() == user.getHasSignIn()
                    && eduTrainningRecordUserMapper.selectPassExamByUserId(user.getUserId(), examId) > 0) {
                eduTrainningRecordUserMapper.updateTrainingResultByUserIds(user.getTrainningRecordId(), "Y", ids);
            } else {
                eduTrainningRecordUserMapper.updateTrainingResultByUserIds(user.getTrainningRecordId(), "N", ids);
            }
        });
    }

    /**
     * 培训合格标准为学完课程+考试通过
     *
     * @param users
     */
    private void finishCourseAndPassExam(List<EduTrainningRecordUser> users, Long examId) {
        users.forEach(user -> {
            List<Long> ids = new ArrayList<>(1);
            ids.add(user.getUserId());
            //是否学完课程
            if (isFinishCourse(user.getUserId(), user.getTrainningRecordId())
                    && eduTrainningRecordUserMapper.selectPassExamByUserId(user.getUserId(), examId) > 0) {
                eduTrainningRecordUserMapper.updateTrainingResultByUserIds(user.getTrainningRecordId(), "Y", ids);
            } else {
                eduTrainningRecordUserMapper.updateTrainingResultByUserIds(user.getTrainningRecordId(), "N", ids);
            }
        });
    }

    /**
     * 用户是否完成培训下的课程
     *
     * @param userId
     * @param trainingId
     * @return
     */
    private Boolean isFinishCourse(Long userId, Long trainingId) {
        List<Long> trainingCoursewares = eduTrainningRecordMapper.selectCoursewareByTrainingId(trainingId);
        List<Long> learns = eduTrainningRecordUserMapper.selectUserLearnCourseware(userId, trainingCoursewares, 1,trainingId);
        //是否学完课程
        return new HashSet<>(learns).containsAll(trainingCoursewares);
    }

    /**
     * 培训合格标准为学完课程
     *
     * @param users
     */
    private void finishCourse(List<EduTrainningRecordUser> users) {
        users.forEach(user -> {
            List<Long> ids = new ArrayList<>(1);
            ids.add(user.getUserId());
            if (isFinishCourse(user.getUserId(), user.getTrainningRecordId())) {
                eduTrainningRecordUserMapper.updateTrainingResultByUserIds(user.getTrainningRecordId(), "Y", ids);
            } else {
                eduTrainningRecordUserMapper.updateTrainingResultByUserIds(user.getTrainningRecordId(), "N", ids);
            }
        });
    }

    /**
     * 培训合格标准为考试通过
     *
     * @param users
     */
    private void passExam(List<EduTrainningRecordUser> users, Long examId) {
        users.forEach(user -> {
            List<Long> ids = new ArrayList<>(1);
            ids.add(user.getUserId());
            if (eduTrainningRecordUserMapper.selectPassExamByUserId(user.getUserId(), examId) > 0) {
                eduTrainningRecordUserMapper.updateTrainingResultByUserIds(user.getTrainningRecordId(), "Y", ids);
            } else {
                eduTrainningRecordUserMapper.updateTrainingResultByUserIds(user.getTrainningRecordId(), "N", ids);
            }
        });
    }

    /**
     * 培训合格标准为完成签到
     *
     * @param users
     */
    private void finishSign(List<EduTrainningRecordUser> users) {
        users.forEach(user -> {
            List<Long> ids = new ArrayList<>(1);
            ids.add(user.getUserId());
            if (TrainingUserSignStatus.OFFLINE.getId() == user.getHasSignIn()) {
                eduTrainningRecordUserMapper.updateTrainingResultByUserIds(user.getTrainningRecordId(), "Y", ids);
            } else {
                eduTrainningRecordUserMapper.updateTrainingResultByUserIds(user.getTrainningRecordId(), "N", ids);
            }
        });
    }

}
